package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.Tuition;

/**
 * @author 袁小弘
 * @describe 学费DAO层接口
 * @date 2020-01-01
 */
@Repository
public interface TuitionDao extends JpaRepository<Tuition, Integer> {

    @Transactional
    @Modifying
    int deleteByTuitionId(Integer tuitionId);

    List<Tuition> findByTuitionId(Integer tuitionId);

    @Query(nativeQuery = true, value = "SELECT tui.*,u.account as userName,u.nickName,c.className,t.termName,ct.typeName\r\n" +
            "FROM ((((tuition tui INNER JOIN student_class sc ON tui.student_class_Id = sc.student_class_Id)\r\n" +
            "INNER JOIN users u ON sc.user_Id = u.user_Id)\r\n" +
            "INNER JOIN classes c ON sc.class_Id = c.class_Id )\r\n" +
            "INNER JOIN term t ON t.term_Id = c.term_Id)\r\n" +
            "INNER JOIN charge_type ct ON ct.charge_type_Id = tui.charge_type_Id")
//	@Query("SELECT new com.example.viewInfo.TermAndTuitionViewInfo(te,tu) FROM Term te,Tuition tu WHERE te.termId = tu.termId ")
    List<Map<String, Object>> findTermAndTuitionViewInfo();

    @Transactional
    @Modifying
    int deleteByTuitionIdIn(List<Integer> listId);

    @Query(nativeQuery = true, value = "SELECT sc.student_class_Id,u.account as userName,u.nickName,ct.typeName,c.className,t.termName \r\n" +
            "FROM (((users u INNER JOIN student_class sc ON u.user_Id = sc.user_Id)\r\n" +
            "INNER JOIN classes c ON c.class_Id = sc.class_Id)\r\n" +
            "INNER JOIN class_type ct ON ct.class_type_Id = c.class_type_Id)\r\n" +
            "INNER JOIN term t ON t.term_Id = c.term_Id\r\n" +
            "WHERE sc.student_class_Id not in (SELECT t.student_class_Id FROM tuition t )")
    List<Map<String, Object>> queryStudentIdNotInTuition();

    @Query(nativeQuery = true, value = "SELECT tui.*,u.account,u.nickName,c.className,t.termName,ct.typeName\r\n" +
            "FROM ((((tuition tui INNER JOIN student_class sc ON tui.student_class_Id = sc.student_class_Id)\r\n" +
            "INNER JOIN users u ON sc.user_Id = u.user_Id)\r\n" +
            "INNER JOIN classes c ON sc.class_Id = c.class_Id )\r\n" +
            "INNER JOIN term t ON t.term_Id = c.term_Id)\r\n" +
            "INNER JOIN charge_type ct ON ct.charge_type_Id = tui.charge_type_Id \r\n" +
            " WHERE t.termName like CONCAT('%',:termName,'%')")
    List<Map<String, Object>> findByTermNameLike(@Param("termName") String termName);

    @Query(nativeQuery = true, value = "SELECT tui.*,u.account,u.nickName,c.className,t.termName,ct.typeName\r\n" +
            "FROM ((((tuition tui INNER JOIN student_class sc ON tui.student_class_Id = sc.student_class_Id)\r\n" +
            "INNER JOIN users u ON sc.user_Id = u.user_Id)\r\n" +
            "INNER JOIN classes c ON sc.class_Id = c.class_Id )\r\n" +
            "INNER JOIN term t ON t.term_Id = c.term_Id)\r\n" +
            "INNER JOIN charge_type ct ON ct.charge_type_Id = tui.charge_type_Id\r\n" +
            "WHERE tui.price like CONCAT('%',:price,'%')")
    List<Map<String, Object>> findByPriceLike(@Param("price") Integer price);


    @Query(nativeQuery = true, value = "SELECT tui.*,u.account,u.nickName,c.className,t.termName,ct.typeName\r\n" +
            "FROM ((((tuition tui INNER JOIN student_class sc ON tui.student_class_Id = sc.student_class_Id)\r\n" +
            "INNER JOIN users u ON sc.user_Id = u.user_Id)\r\n" +
            "INNER JOIN classes c ON sc.class_Id = c.class_Id )\r\n" +
            "INNER JOIN term t ON t.term_Id = c.term_Id)\r\n" +
            "INNER JOIN charge_type ct ON ct.charge_type_Id = tui.charge_type_Id\r\n" +
            "WHERE u.account like CONCAT('%',:userName,'%')")
    List<Map<String, Object>> findByUserNameLike(@Param("userName") String userName);

    @Query(nativeQuery = true, value = "SELECT tui.*,u.account,u.nickName,c.className,t.termName,ct.typeName\r\n" +
            "FROM ((((tuition tui INNER JOIN student_class sc ON tui.student_class_Id = sc.student_class_Id)\r\n" +
            "INNER JOIN users u ON sc.user_Id = u.user_Id)\r\n" +
            "INNER JOIN classes c ON sc.class_Id = c.class_Id )\r\n" +
            "INNER JOIN term t ON t.term_Id = c.term_Id)\r\n" +
            "INNER JOIN charge_type ct ON ct.charge_type_Id = tui.charge_type_Id\r\n" +
            "WHERE u.nickName like CONCAT('%',:nickName,'%')")
    List<Map<String, Object>> findByNickNameLike(@Param("nickName") String nickName);

    @Query(nativeQuery = true, value = "SELECT tui.*,u.account,u.nickName,c.className,t.termName,ct.typeName\r\n" +
            "FROM ((((tuition tui INNER JOIN student_class sc ON tui.student_class_Id = sc.student_class_Id)\r\n" +
            "INNER JOIN users u ON sc.user_Id = u.user_Id)\r\n" +
            "INNER JOIN classes c ON sc.class_Id = c.class_Id )\r\n" +
            "INNER JOIN term t ON t.term_Id = c.term_Id)\r\n" +
            "INNER JOIN charge_type ct ON ct.charge_type_Id = tui.charge_type_Id\r\n" +
            "WHERE tui.status like CONCAT('%',:status,'%')")
    List<Map<String, Object>> findByStatusLike(@Param("status") String status);

    @Query(nativeQuery = true, value = "SELECT tui.*,u.account,u.nickName,c.className,t.termName,ct.typeName\r\n" +
            "FROM ((((tuition tui INNER JOIN student_class sc ON tui.student_class_Id = sc.student_class_Id)\r\n" +
            "INNER JOIN users u ON sc.user_Id = u.user_Id)\r\n" +
            "INNER JOIN classes c ON sc.class_Id = c.class_Id )\r\n" +
            "INNER JOIN term t ON t.term_Id = c.term_Id)\r\n" +
            "INNER JOIN charge_type ct ON ct.charge_type_Id = tui.charge_type_Id\r\n" +
            "WHERE c.className like CONCAT('%',:className,'%')")
    List<Map<String, Object>> findByClassNameLike(@Param("className") String className);

    @Query(nativeQuery = true, value = "SELECT tui.*,u.user_Id,u.account as userName,u.nickName,c.className,t.termName,ct.typeName \r\n" +
            "FROM ((((tuition tui INNER JOIN student_class sc ON tui.student_class_Id = sc.student_class_Id)\r\n" +
            "INNER JOIN users u ON sc.user_Id = u.user_Id)\r\n" +
            "INNER JOIN classes c ON sc.class_Id = c.class_Id ) \r\n" +
            "INNER JOIN term t ON t.term_Id = c.term_Id) \r\n" +
            "INNER JOIN charge_type ct ON ct.charge_type_Id = tui.charge_type_Id\r\n" +
            "WHERE u.user_Id = :userId")
    List<Map<String, Object>> selectTuitionByUserId(@Param("userId") Integer userId);


}
